// pages/search/index.js
import { request } from '../../request/index'
Page({
  data: {
    searchList: [],
    isFocus: false,
    value: ''
  },
  timeId: -1,
  async getSearchList(value) {
    const { data: res } = await request({
      url: '/goods/qsearch',
      data: {
        query: value
      }
    })
    if (res.meta.status !== 200) return
    this.setData({
      searchList: res.message
    })
  },
  search(e) {
    clearTimeout(this.timeId)
    const { value } = e.detail
    if (!value.trim()) {
      this.clear()
      return
    }
    this.setData({
      isFocus: true
    })
    this.timeId = setTimeout(() => {
      this.getSearchList(value)
    }, 1000)
  },
  clear() {
    this.setData({
      searchList: [],
      isFocus: false,
      value: ''
    })
  }
})